package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.LanguageRate;
import com.example.demo.mapper.LanguageRateMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/***
 ** Create by weiweiweiweiwei on 2021/8/9
 */
@RestController
@RequestMapping("/languageRate")
public class LanguageRateController {
    @Resource
    LanguageRateMapper languageRateMapper;

    @PostMapping  //增
    public Result<?> save(@RequestBody LanguageRate languageRate) {
        languageRateMapper.insert(languageRate);
        return Result.success();
    }

    @DeleteMapping("/{id}") //删
    public Result<?> delete(@PathVariable long id) {
        languageRateMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping //查
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "20") Integer pageSize,
                                  @RequestParam(defaultValue = "",required = false) String search) {
        LambdaQueryWrapper<LanguageRate> wrapper = Wrappers.<LanguageRate>lambdaQuery();
        System.out.println("-----------------search:"+search);
        if(StrUtil.isNotBlank(search)) {
            wrapper.like(LanguageRate::getId,search);
        }
        Page<LanguageRate> LanguagePage = languageRateMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(LanguagePage);
    }


    @PutMapping //改
    public Result<?> update(@RequestBody LanguageRate languageRate) {
        languageRateMapper.updateById(languageRate);
        return Result.success();
    }
}
